home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-11-03 | 2.9 KB | 122 lines | [TEXT/KAHL] |
- #include <qsort++.h>
- #include "CDRVRArray.h"
-
- #define N_TXTOFF(ex) sizeof(struct exec)
- #define N_DATOFF(ex) (N_TXTOFF(ex) + (ex).a_text)
- #define N_SYMOFF(ex) (N_DATOFF(ex) + (ex).a_data + (ex).a_trsize + (ex).a_drsize)
- #define N_STROFF(ex) (N_SYMOFF(ex) + (ex).a_syms)
-
- void CDRVRArray::IResHandle( Handle itsData)
- {
- int i,nsyms;
- struct nlist *sp;
- char *strtbl;
-
- HLock(itsData);
- execp = (void *)(244+*itsData);
-
- nsyms = execp->a_syms / sizeof (struct nlist);
-
- IArray(sizeof (struct nlist));
-
- Resize(nsyms);
-
- strtbl = (char *)execp+N_STROFF(*execp);
- sp = (void *)((char *)execp+N_SYMOFF (*execp));
-
- for (i = 1; i <= nsyms; i++)
- {
- struct nlist newlist = *sp;
- if (newlist.n_un.n_strx == 0)
- newlist.n_un.n_name = "";
- else if (newlist.n_un.n_strx < 0 /* || newlist.n_un.n_strx > strsize */)
- newlist.n_un.n_name = "<bad string table index>";
- else
- newlist.n_un.n_name = strtbl + newlist.n_un.n_strx;
- InsertAtIndex(&newlist, 1);
- sp++;
- }
- }
-
- /******************************************************************************
- Sort
- ******************************************************************************/
-
- Boolean CDRVRArray::cCompNumerically; // compare for Numerically sort if true
- CDRVRArray *CDRVRArray::cCurrArray; // array being sorted
-
- /* Comparison functions for sorting symbols. */
-
- #define reverse_sort 0
-
- static int
- alphacompare (struct nlist *sym1, struct nlist *sym2)
- {
- int strcmp(char *,char *);
- if (reverse_sort)
- {
- if (!sym2->n_un.n_name)
- {
- if (sym1->n_un.n_name) return -1;
- else return 0;
- }
- if (!sym1->n_un.n_name) return 1;
- return strcmp (sym2->n_un.n_name, sym1->n_un.n_name);
- }
- else
- {
- if (!sym1->n_un.n_name)
- {
- if (sym2->n_un.n_name) return -1;
- else return 0;
- }
- if (!sym2->n_un.n_name) return 1;
- return strcmp (sym1->n_un.n_name, sym2->n_un.n_name);
- }
- }
-
-
- static int
- valuecompare (
- struct nlist *sym1, struct nlist *sym2)
- {
- if (reverse_sort)
- return sym2->n_value - sym1->n_value;
- else
- return sym1->n_value - sym2->n_value;
- }
-
- void CDRVRArray::swap( size_t i, size_t j)
- {
- CDRVRArray::cCurrArray->Swap( i+1, j+1);
- }
-
- int CDRVRArray::CompareStrings( size_t index1, size_t index2)
- {
- unsigned char *items = (unsigned char*) *CDRVRArray::cCurrArray->hItems;
-
- int (*func)(void *,void *) = (void *)(cCompNumerically ? valuecompare : alphacompare);
-
- return func( items + CDRVRArray::cCurrArray->ItemOffset( index1+1),
- items + CDRVRArray::cCurrArray->ItemOffset( index2+1));
- }
-
- void CDRVRArray::Sort( Boolean sort_numerically)
- {
- SignedByte state = HGetState( hItems);
-
- /* qsort won't move memory, but LoadSeg can */
-
- cCompNumerically = sort_numerically;
- cCurrArray = this;
-
- HLock( hItems);
-
- qsortplusplus( numItems, CompareStrings, swap); // TCL 1.1.3 MER 11/7/92, 3/15/93
-
- HSetState( hItems, state);
-
- cCurrArray = NULL;
-
- } /* CDRVRArray::Sort */
-